package demo;

import com.lijinjiang.beautyeye.BeautyEyeLNFHelper;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @ClassName demo.AllComponentDemo
 * @Description TODO
 * @Author Li
 * @Date 2022/8/20 23:05
 * @ModifyDate 2022/8/20 23:05
 * @Version 1.0
 */
public class AllComponentDemo {
    public static void main(String[] args) {
        try {
            BeautyEyeLNFHelper.frameBorderStyle = BeautyEyeLNFHelper.FrameBorderStyle.generalNoTranslucencyShadow;
            com.lijinjiang.beautyeye.BeautyEyeLNFHelper.launchBeautyEyeLNF();
            // 设置 JTabbedPane 的左缩进
            UIManager.put("TabbedPane.tabAreaInsets"
                    , new javax.swing.plaf.InsetsUIResource(0, 0, 0, 0));
        } catch (Exception e) {

        }
        JFrame frame = new JFrame();
        frame.setTitle("Look And Feel 展示");
        frame.setSize(800, 600); // 大小
        addMenuBar(frame);
        setContentPane(frame);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null); // 居中
        frame.setVisible(true); // 可见
    }

    // 菜单栏
    public static void addMenuBar(JFrame frame) {
        JMenuBar menuBar = new JMenuBar(); // 菜单栏
        JMenu lafMenu = new JMenu("外观"); // 菜单
        ButtonGroup buttonGroup = new ButtonGroup();
        JCheckBoxMenuItem metalItem = new JCheckBoxMenuItem("Metal"); // 菜单项
        JCheckBoxMenuItem nimbusItem = new JCheckBoxMenuItem("Nimbus"); // 菜单项
        JCheckBoxMenuItem motifItem = new JCheckBoxMenuItem("CDE/Motif"); // 菜单项
        JCheckBoxMenuItem windowsItem = new JCheckBoxMenuItem("Windows"); // 菜单项
        JCheckBoxMenuItem classicItem = new JCheckBoxMenuItem("Windows Classic"); // 菜单项
        buttonGroup.add(metalItem);
        buttonGroup.add(nimbusItem);
        buttonGroup.add(motifItem);
        buttonGroup.add(windowsItem);
        buttonGroup.add(classicItem);
        metalItem.setState(true);
        metalItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    UIManager.setLookAndFeel("javax.swing.plaf.metal.MetalLookAndFeel");
                    SwingUtilities.updateComponentTreeUI(frame.getContentPane());
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });
        nimbusItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
                    SwingUtilities.updateComponentTreeUI(frame.getContentPane());
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });
        motifItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.motif.MotifLookAndFeel");
                    SwingUtilities.updateComponentTreeUI(frame.getContentPane());
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });
        windowsItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
                    SwingUtilities.updateComponentTreeUI(frame.getContentPane());
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });
        classicItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
                    SwingUtilities.updateComponentTreeUI(frame.getContentPane());
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
        });

        lafMenu.add(metalItem);
        lafMenu.add(nimbusItem);
        lafMenu.add(motifItem);
        lafMenu.add(windowsItem);
        lafMenu.add(classicItem);
        menuBar.add(lafMenu);
        frame.setJMenuBar(menuBar);
    }

    public static void setContentPane(JFrame frame) {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(3, 3, 10, 10));

        // 第一行
        panel.add(getTabbedPane());
        panel.add(getScrollPane());
        panel.add(getSplitPane());

        // 第二行
        panel.add(getTable());
        panel.add(getTree());
        panel.add(getList());

        // 第三行
        panel.add(getButton());
        panel.add(getProgressBar());
        panel.add(getSlider());

        frame.setContentPane(panel);
    }

    // 添加 JTabbedPane 模块
    public static JComponent getTabbedPane() {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        JTabbedPane tabbedPane = new JTabbedPane();
        JPanel panel1 = new JPanel();
        panel1.add(new JLabel("语文"));
        tabbedPane.addTab("语文", panel1);
        JPanel panel2 = new JPanel();
        panel2.add(new JLabel("数学"));
        tabbedPane.addTab("数学", panel2);
        JPanel panel3 = new JPanel();
        panel3.add(new JLabel("英语"));
        tabbedPane.addTab("英语", panel3);
        panel.add(tabbedPane);
        return myPanel("JTabbedPane", panel);
    }

    // 添加 JScrollPane 模块
    public static JComponent getScrollPane() {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        JTextArea textArea = new JTextArea();
        textArea.setText("　　我说道，“爸爸，你走吧。”他往车外看了看说，“我买几个橘子去。你就在此地，不要走动。”我看那边月台的栅栏外有几个卖东西的等着顾客。走到那边月台，须穿过铁道，须跳下去又爬上去。父亲是一个胖子，走过去自然要费事些。我本来要去的，他不肯，只好让他去。我看见他戴着黑布小帽，穿着黑布大马褂，深青布棉袍，蹒跚地走到铁道边，慢慢探身下去，尚不大难。可是他穿过铁道，要爬上那边月台，就不容易了。他用两手攀着上面，两脚再向上缩；他肥胖的身子向左微倾，显出努力的样子。这时我看见他的背影，我的泪很快地流下来了。我赶紧拭干了泪。怕他看见，也怕别人看见。我再向外看时，他已抱了朱红的橘子往回走了。过铁道时，他先将橘子散放在地上，自己慢慢爬下，再抱起橘子走。到这边时，我赶紧去搀他。他和我走到车上，将橘子一股脑儿放在我的皮大衣上。于是扑扑衣上的泥土，心里很轻松似的。过一会儿说，“我走了，到那边来信！”我望着他走出去。他走了几步，回过头看见我，说，“进去吧，里边没人。”等他的背影混入来来往往的人里，再找不着了，我便进来坐下，我的眼泪又来了。");
        textArea.setLineWrap(true); // 自动换行
        JScrollPane scrollPane = new JScrollPane(textArea);
        scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); // 设置垂直滚动条一直存在
        scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER); // 设置水平滚动条一直不显示
        panel.add(scrollPane);
        return myPanel("JScrollPane", panel);
    }

    // 添加 JSplitPane 模块
    public static JComponent getSplitPane() {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        JSplitPane splitPane = new JSplitPane();
        JPanel leftPane = new JPanel();
        JPanel rightPane = new JPanel();
        leftPane.setBackground(Color.BLUE);
        rightPane.setBackground(Color.ORANGE);
        splitPane.setLeftComponent(leftPane);
        splitPane.setRightComponent(rightPane);
        splitPane.setDividerLocation(60);
        panel.add(splitPane);
        return myPanel("JSplitPane", panel);
    }

    // 添加 JTable 模块
    public static JComponent getTable() {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        String[] columnNames = {"编号", "名称", "年度", "导演"};
        Object[][] data = {
                {"1", "仙剑奇侠传", "2005", "李国立、吴锦源、梁胜权、麦贯之"},
                {"2", "亮剑", "2005", "陈健、张前"},
                {"3", "武林外传", "2006", "尚敬"},
                {"4", "爱情公寓", "2009", "韦正"},
                {"5", "隐秘的角落", "2020", "辛爽"}
        };
        JTable table = new JTable(data, columnNames);
        table.setRowHeight(30); // 设置行高
        // 指定第 1,2 列的列宽
        table.getColumnModel().getColumn(0).setPreferredWidth(50);
        table.getColumnModel().getColumn(1).setPreferredWidth(120);

        // 设置第 1,3 列设置居中对其
        DefaultTableCellRenderer cellRenderer = new DefaultTableCellRenderer();
        cellRenderer.setHorizontalAlignment(JTextField.CENTER);
        table.getColumnModel().getColumn(0).setCellRenderer(cellRenderer);
        table.getColumnModel().getColumn(2).setCellRenderer(cellRenderer);

        JScrollPane scrollPane = new JScrollPane(table); // table 必须添加到 JScrollPane，否则不显示列标题
        panel.add(scrollPane);
        return myPanel("JTable", panel);
    }

    // 添加 JTree 模块
    public static JComponent getTree() {
        JPanel panel = new JPanel();
        JScrollPane scrollPane = new JScrollPane();
        panel.setLayout(new BorderLayout());
        JTree tree = new JTree();
        scrollPane.setViewportView(tree);
        panel.add(scrollPane);
        return myPanel("JTree", panel);
    }

    // 添加 JList 模块
    public static JComponent getList() {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        JScrollPane scrollPane = new JScrollPane();
        String[] provinces = {"北京市（京）", "天津市（津）", "上海市（沪）", "重庆市（渝）", "河北省（冀）", "河南省（豫）", "云南省（云）", "辽宁省（辽）", "黑龙江省（黑）", "湖南省（湘）", "安徽省（皖）", "山东省（鲁）", "新疆维吾尔（新）", "江苏省（苏）", "浙江省（浙）", "江西省（赣）", "湖北省（鄂）", "广西壮族（桂）", "甘肃省（甘）", "山西省（晋）", "内蒙古（蒙）", "陕西省（陕）", "吉林省（吉）", "福建省（闽）", "贵州省（贵）", "广东省（粤）", "青海省（青）", "西藏（藏）", "四川省（川）", "宁夏回族（宁）", "海南省（琼）", "台湾省（台）", "香港特别行政区", "澳门特别行政区"};
        JList list = new JList(provinces);
        scrollPane.setViewportView(list);
        panel.add(scrollPane);
        return myPanel("JScrollPane", panel);
    }

    // 添加 JButton 模块
    public static JComponent getButton() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        JPanel buttonPanel = new JPanel();
        buttonPanel.setLayout(new GridLayout(1, 2, 30, 30));
        JButton button1 = new JButton("Start");
        JButton button2 = new JButton("End");
        buttonPanel.add(button1);
        buttonPanel.add(button2);
        panel.add(buttonPanel);
        return myPanel("JButton", panel);
    }

    // 添加 JProgressBar 模块
    public static JComponent getProgressBar() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        JProgressBar progressBar = new JProgressBar();
        progressBar.setValue(60);
        progressBar.setStringPainted(true);
        panel.add(progressBar);
        return myPanel("JProgressBar", panel);
    }

    // 添加 JSlider 模块
    public static JComponent getSlider() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridBagLayout());
        JSlider slider = new JSlider(0, 100, 30);
        slider.setMajorTickSpacing(10); // 设置主刻度间隔
        slider.setMinorTickSpacing(5); // 设置次刻度间隔
        slider.setPaintTicks(true); // 绘制刻度
        slider.setPaintLabels(true); // 绘制标签
        panel.add(slider);
        return myPanel("JSlider", panel);
    }

    // 组件组合方法
    public static JComponent myPanel(String title, JPanel bottomPanel) {
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        JPanel topPanel = new JPanel();
        JLabel label = new JLabel(title);
        topPanel.add(label);
        panel.add(topPanel, BorderLayout.NORTH);
        panel.add(bottomPanel, BorderLayout.CENTER);
        panel.setBorder(BorderFactory.createLoweredBevelBorder());
        return panel;
    }

}
